<template>
  <div class="login-wrap">
    <div class="login-box">
      <div class="login-title">Vue3_admin</div>
      <el-form
        :model="userInfo"
        :rules="rules"
        ref="loginForms"
        class="login-content"
      >
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            v-model="userInfo.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            v-model="userInfo.password"
            show-password
          ></el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button :loading="loading" type="primary" @click="submitForm()">
            登录
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { Lock, User, View } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/modules/user'
//引入获取当前时间的函数
import { getTime } from '@/utils/time'
//获取路由器
let $router = useRouter()
//路由对象
let $route = useRoute()
const userInfo = reactive({
  username: 'dongzq',
  password: '123456',
})
const rules = {
  username: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur',
    },
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur',
    },
  ],
}
onMounted(() => {})

const loginForms = ref()

// 按钮loading
const loading = ref(false)
// 登录
const userStore = useUserStore()
const submitForm = async () => {
  //保证全部表单相校验通过再发请求
  await loginForms.value.validate()
  //加载效果:开始加载
  loading.value = true
  try {
    //编程式导航跳转到展示数据首页
    //判断登录的时候,路由路径当中是否有query参数，如果有就往query参数挑战，没有跳转到首页
    // let redirect = $route.query.redirect
    // $router.push({ path: (redirect as string) || '/' })
    $router.push('/home')
    //登录成功提示信息
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `HI,${getTime()}好`,
    })
    //登录成功加载效果也消失
    loading.value = false
  } catch (error: any) {
    console.log('失败')
    //登录失败加载效果消息
    loading.value = false
    //登录失败的提示信息
    ElNotification({
      type: 'error',
      message: error.message,
    })
  }
}

// 处理引入静态图片
const getImageUrl = (name: string) => {
  return new URL(`../../assets/images/${name}`, import.meta.url).href
}
</script>

<style lang="scss" scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100vh;
  background: #ddd;
  display: flex;
  justify-content: center;
  align-items: center;

  .image {
    width: 240px;
    height: 80px;
    position: absolute;
    z-index: 99;
    top: 40px;
    left: 40px;
  }

  .login-box {
    width: 480px;
    border-radius: 20px;
    background: rgba(255, 255, 255, 0.8);
    overflow: hidden;

    .login-title {
      width: 100%;
      line-height: 60px;
      text-align: center;
      font-size: 26px;
      font-weight: 600;
      color: #000;
      border-bottom: 1px solid #ddd;
    }

    .login-content {
      padding: 30px 30px;

      .login-btn {
        text-align: center;

        button {
          width: 100%;
          height: 36px;
          margin-bottom: 10px;
        }
      }
    }
  }
}

:deep(.el-form-item) {
  padding-right: 0;
  margin: 20px 0;
  color: #454545;
  background: transparent;

  .el-button {
    span {
      font-size: 20px;
    }
  }
}

:deep(.el-input) {
  box-sizing: border-box;

  input {
    height: 45px;
    line-height: 45px;
    background: #fff;
    border-top: 0;
    border-left: 0;
    border-right: 0;
    border-radius: 0;
  }
}
</style>
